import { defineConfig } from 'vite';
import tailwindcss from 'tailwindcss'; // 使用 ESM 导入
import autoprefixer from 'autoprefixer';
import react from '@vitejs/plugin-react';
import { createSvgIconsPlugin, createSymbolId } from 'vite-plugin-svg-icons';
import path from 'path';
import checker from 'vite-plugin-checker';

const host = process.env.TAURI_DEV_HOST;
// https://vitejs.dev/config/
export default defineConfig(async () => ({
    plugins: [
        react(),
        createSvgIconsPlugin({
            // 指定需要缓存的图标文件夹
            iconDirs: [path.resolve(process.cwd(), 'src/icons/svg')],
            // 指定symbolId格式
            symbolId: `icon-[dir]-[name]`,
            svgoOptions: {
                plugins: [
                    {
                        name: 'removeAttrs',
                        params: {
                            attrs: ['class', 'data-name', 'fill', 'stroke']
                        }
                    }
                ]
            }
        }),
        !process.env.VITEST
            ? checker({
                  typescript: true,
                  eslint: {
                      lintCommand: 'eslint "./src/**/*.{ts,tsx}"',
                      useFlatConfig: true,
                  },
              })
            : undefined,
    ],
    resolve: {
        alias: {
            '@': path.resolve(__dirname, './src'),
        },
    },
    // 新增 css 配置
    css: {
        postcss: {
            plugins: [
                tailwindcss(), // 直接调用函数
                autoprefixer(),
            ],
        },
        preprocessorOptions: {
            scss: {
                additionalData: `
                $namespace: "sl";
                $elNamespace: "el";
              `,
            },
        },
    },
    clearScreen: false,
    // 2. tauri expects a fixed port, fail if that port is not available
    server: {
        port: 1420,
        strictPort: true,
        host: host || false,
        hmr: host
            ? {
                  protocol: 'ws',
                  host,
                  port: 1421,
              }
            : undefined,
        watch: {
            // 3. tell vite to ignore watching `src-tauri`
            ignored: ['**/src-tauri/**'],
        },
    },
    build: {
        rollupOptions: {
            input: {
                main: './index.html' // 明确指定 HTML 入口
            },
            external: [] // 确保没有将 index.html 设为 external
        },
    },
}));
